<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
//jimport('joomla.environment.uri' );

//add the links to the external files into the head of the webpage (note the 'administrator' in the path, which is not nescessary if you are in the frontend)
$document =& JFactory::getDocument();
$document->addStyleSheet( NGCOM_URL.'/assets/css/ngmenu.css' );

/**
 * NGMenus View
 */
class NGMenuViewNGMenus extends JView
{
	var $modules = array();
	var $titles = array();
	
	/**
	 * NGMenus view display method
	 * @return void
	 */
	function display($tpl = null) 
	{
		// the edit of ngmenu was cancelled, all ngitems not assigned to the menu (ngmenu_id=0) have to be deleted 
		$model = $this->getModel();
		$task = $model->getState('task');
		if ($task == '') {
			JLoader::import('joomla.application.component.modeladmin');
			$ngitem_model = JModelAdmin::getInstance( 'NGItem', 'NGMenuModel' );
			$ngitems = $ngitem_model->getItem('ngmenu_id', '0', $getlist=true);
			foreach ($ngitems as $ngitem){
				$ngitem_model->delete($ngitem->id);
			}
		}
		
		// Get data from the model
		$items = $this->get('Items');
		$pagination = $this->get('Pagination');
 
		// Check for errors.
		if (count($errors = $this->get('Errors'))) 
		{
			JError::raiseError(500, implode('<br />', $errors));
			return false;
		}
		// Assign data to the view
		$this->items = $items;
		$this->pagination = $pagination;

		// Get the module data
		$this->getModules();
		
		// Get the menu titles
		$this->getMenuTitles();
		
		// Set the toolbar
		$this->addToolBar();

		// Display the template
		parent::display($tpl);
	}
	
	/**
	 * Get the module data 
	 */
	protected function getModules() 
	{
		$db = JFactory::getDBO();

		$modules = array();
		foreach($this->items as $row){
			$query = $db->getQuery(true);
			$query->select('m.id, m.title, m.position, m.published, m.access');
			$query->from('#__modules AS m');
			$query->select('a.title AS access_level');
			$query->join('LEFT', '#__viewlevels AS a ON a.id = m.access');
			$query->where("m.id='".$row->module_id."'");
			$db->setQuery($query);
//			echo $db->explain();
			$module = $db->loadObject();
			$this->modules[$row->id] = !empty($module) ? $module : '';
		}
	}
	
	/**
	 * Get the menu titles 
	 */
	protected function getMenuTitles() 
	{
		$db = JFactory::getDBO();

		foreach($this->items as $row){
			$query = $db->getQuery(true);
			$query->select('title');
			$query->from('#__menu_types');
			$query->where("menutype='".$row->name."'");
			$db->setQuery($query);
			$this->titles[$row->id] = $db->loadResult();
		}
	}
	
	/**
	 * Setting the toolbar
	 */
	protected function addToolBar() 
	{
		JToolBarHelper::title(JText::_('COM_NGMENU_MENU_CONFIG'), 'logongm');
		JToolBarHelper::addNew('ngmenu.add');
		JToolBarHelper::editList('ngmenu.edit');
		JToolBarHelper::deleteList('', 'ngmenus.delete');
	}
}
